package com.infoland.afanmall.model.story.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.AppCompatImageView;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.transition.Fade;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.blankj.utilcode.util.ToastUtils;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.infoland.afanmall.R;
import com.infoland.afanmall.base.activity.GlideApp;
import com.infoland.afanmall.base.util.CONST;
import com.infoland.afanmall.base.util.LogKit;
import com.infoland.afanmall.base.util.ToolsKit;
import com.infoland.afanmall.base.util.okhttp.OkHttpKit;
import com.infoland.afanmall.base.view.video.MyJiaoZiVideo;
import com.infoland.afanmall.model.story.adapter.StoryInfoAdapter;
import com.infoland.afanmall.model.story.entity.StoryInfoAdapterEntity;
import com.infoland.afanmall.model.story.entity.StoryInfoEntity;
import com.infoland.afanmall.model.youXuan.activity.GoodInfoActivity;
import com.wyj.cardtantan.CardItemTouchHelperCallback;
import com.wyj.cardtantan.CardLayoutManager;
import com.wyj.cardtantan.OnSwipeListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import cn.jzvd.JZVideoPlayer;

public class StoryInfoActivity extends AppCompatActivity {

    @BindView(R.id.story_video)
    MyJiaoZiVideo storyVideo;
    @BindView(R.id.story_video_layout)
    RelativeLayout storyVideoLayout;
    @BindView(R.id.story_video_text_layout)
    ConstraintLayout storyVideoTextLayout;
    @BindView(R.id.story_info_img)
    ImageView storyInfoImg;
    @BindView(R.id.story_info_img_txt1)
    AppCompatTextView storyInfoImgTxt1;
    @BindView(R.id.story_info_img_txt2)
    AppCompatTextView storyInfoImgTxt2;
    @BindView(R.id.story_info_img_layout)
    RelativeLayout storyInfoImgLayout;
    @BindView(R.id.video_text1)
    AppCompatTextView videoText1;
    @BindView(R.id.video_text2)
    AppCompatTextView videoText2;
    @BindView(R.id.video_back)
    LinearLayout videoBack;
    StoryInfoAdapter storyInfoAdapter;
    @BindView(R.id.recycler_content)
    RecyclerView recyclerContent;
    @BindView(R.id.story_info_bottom_img)
    AppCompatImageView storyInfoBottomImg;
    @BindView(R.id.story_info_bottom_txt1)
    AppCompatTextView storyInfoBottomTxt1;
    @BindView(R.id.story_info_bottom_txt2)
    AppCompatTextView storyInfoBottomTxt2;
    private int goodId;

    private int getLayout() {
        return R.layout.story_info_activity;
    }

    //入口
    private void initData() {
        recyclerContent.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        storyVideo.setVideoRestart(() -> {
//            getSupportActionBar().hide();
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        });
        Bundle extras = this.getIntent().getExtras();
        if (extras != null) {
            int id = extras.getInt("id");
            getStory(id);
        } else {
            ToastUtils.showShort("数据异常");
            finishAfterTransition();
        }
    }

    //开始查询**/
    private void getStory(int id) {
        JSONObject param = new JSONObject();
        param.put("storyId", id);
        new OkHttpKit().send(
                response200 -> {
                    StoryInfoEntity stioryInfoEntity = JSON.parseObject(response200, StoryInfoEntity.class);
                    if (stioryInfoEntity.isSuccess()) {
                        updateUi(stioryInfoEntity.getObject());
                    }
                },
                success401 -> {
                    ToastUtils.showShort("数据加载失败");
                },
                error -> {
                    ToastUtils.showShort("数据加载失败");
                }
        ).post(CONST.STORY_GET_BY_ID, param, this);

    }

    //更新界面**/
    private void updateUi(StoryInfoEntity.ObjectBean object) {
        /*** 释放展示类型**/
        if ("image".equals(object.getS_background_type())) {
            ToolsKit.showView(storyInfoImgLayout);
            ToolsKit.setImgActivity(StoryInfoActivity.this, object.getS_background_image(), storyInfoImg);
            ToolsKit.setText(storyInfoImgTxt1, object.getS_title());
            ToolsKit.setText(storyInfoImgTxt2, object.getS_description());
            ToolsKit.setTextColor(storyInfoImgTxt1, object.getS_title_color());
        } else if ("video".equals(object.getS_background_type())) {
            ToolsKit.showView(storyVideoLayout);
            ToolsKit.showView(storyVideoTextLayout);
            ToolsKit.showView(videoBack);
            ToolsKit.setVideo(storyVideo, object.getS_background_video());
            ToolsKit.setVideoImg(storyVideo, object.getS_background_image());
            ToolsKit.setText(videoText1, object.getS_title());
            ToolsKit.setTextColor(videoText1, object.getS_title_color());
            ToolsKit.setText(videoText2, object.getS_description());
        }
        /*** 设置中间适配器**/
        List<StoryInfoAdapterEntity> adapterEntities =
                JSONArray.parseArray(JSONObject.toJSONString(object.getS_context()), StoryInfoAdapterEntity.class);
        StoryInfoAdapterEntity storyInfoAdapterEntity = new StoryInfoAdapterEntity();
        storyInfoAdapterEntity.setType("");
        adapterEntities.add(storyInfoAdapterEntity);
        storyInfoAdapter = new StoryInfoAdapter(adapterEntities);
        storyInfoAdapter.setGoodId(goodId);
        storyInfoAdapter.setStoryInfoActivity(this);
        recyclerContent.setAdapter(storyInfoAdapter);

        /*** 设置底部商品数据**/
        List<StoryInfoEntity.ObjectBean.GoodsListBean> goodsList = object.getGoodsList();
        if (goodsList.size() > 0) {
            StoryInfoEntity.ObjectBean.GoodsListBean goodsListBean = goodsList.get(0);
            goodId = goodsListBean.getId();
            ToolsKit.setImgActivity(StoryInfoActivity.this, goodsListBean.getF_url(), storyInfoBottomImg);
            ToolsKit.setText(storyInfoBottomTxt1, goodsListBean.getG_name());
            ToolsKit.setText(storyInfoBottomTxt2, goodsListBean.getG_introduction());
        }

    }

    @OnClick({R.id.story_info_img_back, R.id.video_back, R.id.story_info_bottom_by})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            /***退出 **/
            case R.id.story_info_img_back:
                finishAfterTransition();
                break;
            /***退出 **/
            case R.id.video_back:
                finishAfterTransition();
                break;
            /*** 跳转商品详情**/
            case R.id.story_info_bottom_by:
                Bundle b = new Bundle();
                b.putInt("id", goodId);
                /*** 去品详情页**/
                ToolsKit.goDh(this, GoodInfoActivity.class, b);
                break;

        }
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setEnterTransition(new Fade());
        setContentView(getLayout());
        ButterKnife.bind(this);
        initData();
    }

    @Override
    protected void onDestroy() {
        ToolsKit.deleteOKHttp(this);
        super.onDestroy();
    }

    @Override
    public void onBackPressed() {
        JZVideoPlayer.releaseAllVideos();
        super.onBackPressed();
    }

    @Override
    public void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }
}
